﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProjectA.Controls;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using ProyectA;

namespace ProjectA.Modules.Costos.Orde_Compra_Servicios
{
    public class cWindowOC_OS : cWindow
    {
        public cWindow panelCostosOCOS;
        public int CentrosCostosId { get; set; }
        private BindingSource bsOrdenes;
        wsGeneral.WsGeneral wsGeneral;

        private enum PestañaModuloCostos { Factura, Presupuesto, Ordenes }

        private PestañaModuloCostos PestañaActual { get; set; }

        private float TotalOrdenCS{get;set;}

        DataSet dsEstructura;
        List<Item> Items;
    
        public cWindowOC_OS(frmPrincipal frmPrincipal, Point pPoint, Size sSize)
        {
            this.frmPrincipal = frmPrincipal;
            this.BackColor = Color.Transparent;
            this.Location = pPoint;
            this.Size = sSize;
            InitializeComponent();
            InitComponent();
            this.btTabInformacionGen.Checked = true;
            wsGeneral = new wsGeneral.WsGeneral();
            dsEstructura = new DataSet();
            Items = new List<Item>();
        }

        private void LoadOrdenesCS()
        {
            DataSet dsPoSo = new DataSet();
            DataSet dsElementosPoSo = new DataSet();
            DataSet dsProveedores = new DataSet();
            string sErr;
            if (wsGeneral.GetPoSo(null, null, null, null, null, null, null, null, out dsPoSo, out sErr))
            {
                if (dsPoSo.Tables[0].Rows.Count > 0)
                {
                    if (wsGeneral.GetItemsPorOrden(null, int.Parse(dsPoSo.Tables[0].Rows[0]["ID"].ToString()), null, null, null, null, out dsElementosPoSo, out sErr))
                    {
                        dsPrincipal.Tables["OrdenCompraServicios"].Clear();
                        dsPrincipal.Tables["ElementosAsociadosOrdenes"].Clear();
                        dsPrincipal.Load(dsPoSo.CreateDataReader(), LoadOption.OverwriteChanges, "OrdenCompraServicios");
                        dsPrincipal.Load(dsElementosPoSo.CreateDataReader(), LoadOption.OverwriteChanges, "ElementosAsociadosOrdenes");

                        TotalOrdenCS = float.Parse(dsPoSo.Tables[0].Rows[0]["Valor"].ToString());
                        CalculoAcumulado();
                    }
                }
                else
                {
                    dsPrincipal.Tables["OrdenCompraServicios"].Clear();
                    dsPrincipal.Tables["ElementosAsociadosOrdenes"].Clear();
                    MessageBox.Show("No hay ordenes de compra - sevicios");
                }
            }

            if (wsGeneral.GetProviders(null, null, null, null, null, null, null, null, out dsProveedores, out sErr))
            {
                dsPrincipal.Tables["Proveedores"].Clear();
                dsPrincipal.Load(dsProveedores.CreateDataReader(), LoadOption.OverwriteChanges, "Proveedores");
            }


            if (wsGeneral.GetEstructura(null, CentrosCostosId, null, null, null, null, null, out dsEstructura, out sErr))
            {
                dsPrincipal.Tables["Estructura"].Clear();
                dsPrincipal.Load(dsEstructura.CreateDataReader(), LoadOption.OverwriteChanges, "Estructura");
                ArmandoEstructura(0, null);
                cbItemsEstructura.DataSource = Items;
                cbItemsEstructura.DisplayMember = "Name";
                cbItemsEstructura.ValueMember = "ItemId";

            }
            cbItemsEstructura.SelectedIndex = -1;


        }

        private void bsOrdenes_CurrentChanged(object sender, EventArgs e)
        {
            DataSet dsElementosPoSo = new DataSet();
            string sErr;

            if (tbOrdenesID.Text != "")
            {
                if (wsGeneral.GetItemsPorOrden(null, int.Parse(tbOrdenesID.Text), null, null, null, null, out dsElementosPoSo, out sErr))
                {
                    dsPrincipal.Tables["ElementosAsociadosOrdenes"].Clear();
                    dsPrincipal.Load(dsElementosPoSo.CreateDataReader(), LoadOption.OverwriteChanges, "ElementosAsociadosOrdenes");

                }

                if (tbOrdenValor.Text == "")
                    tbOrdenValor.Text = "0";

                TotalOrdenCS = float.Parse(tbOrdenValor.Text);
                CalculoAcumulado();
            }
        }

        private float fTotalAcumulado;

        private float TotalAcumuladoElementosOrdenesCompraServicios
        {
            get
            {
                return fTotalAcumulado;
            }

            set
            {
                lTotalAcumuladoValor.Text = value.ToString("C");
                lOCSValorRestante.Text = (TotalOrdenCS - value).ToString("C");
                if (value > TotalOrdenCS)
                {
                    //btSalvarOrdenesCS.Enabled = false;
                    btAgregarElementoOrdenCS.Enabled = false;
                    throw new Exception("Los Valores Ingresados son incorrectos por favor reviselos");
                }
                else
                {

                    fTotalAcumulado = value;
                    btAgregarElementoOrdenCS.Text = "Agregar";

                    if (TotalOrdenCS - value == 0)
                    {
                        btAgregarElementoOrdenCS.Text = "Guardar";
                    }
                    else
                    {
                        btAgregarElementoOrdenCS.Enabled = true;
                    }
                }
            }
        }

        private void btAgregarElementoOrdenCS_Click(object sender, EventArgs e)
        {
            if (btAgregarElementoOrdenCS.Text == "Guardar")
            {
                SalvarOrdenCS();
            }
            else
            {
                if (cbItemsEstructura.SelectedIndex != -1)
                {
                    if (TotalAcumuladoElementosOrdenesCompraServicios < TotalOrdenCS)
                    {
                        DataRow dr = dsPrincipal.Tables["ElementosAsociadosOrdenes"].NewRow();
                        dr["ID"] = 0;
                        //dr["PoSoID"] = tbOrdenesID.Text;
                        dr["ItemID"] = cbItemsEstructura.SelectedValue;
                        dr["Descripcion"] = cbItemsEstructura.Text;
                        dr["Cantidad"] = 0;
                        dr["PrecioUD"] = 0;
                        dr["PrecioTotal"] = 0;
                        dsPrincipal.Tables["ElementosAsociadosOrdenes"].Rows.Add(dr);
                        cbItemsEstructura.SelectedIndex = -1;
                    }

                    else
                    {
                        MessageBox.Show("No puede agregar mas elementos a la orden, el valor de los acumulado equivale al valor de la factura agregada",
                            "ProjectA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("Por favor elija un elemento de la estructura",
                           "ProjectA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    //panelModulosTreeview.Left = 0;
                    //btModulosTreeviewMostrar.BackgroundImage = ProyectA.Properties.Resources.bt_flecha_pestaña_i_2;
                }
            }
        }



        private void btSalvarOrdenesCS_Click(object sender, EventArgs e)
        {
            CalculoAcumulado();
            if (TotalAcumuladoElementosOrdenesCompraServicios == 0)
            {
                MessageBox.Show("Por favor ageregue los elementos asociados a la orden",
                           "ProjectA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                TotalOrdenCS = float.Parse(tbOrdenValor.Text);
                CalculoAcumulado();

                //tcOrdenCS.SelectedIndex = 1;
                btTabInformacionGen.Checked = true;
            }
            else
            {
                SalvarOrdenCS();
            }
        }

        cOrdenCompraServicio Orden;

        private void SalvarOrdenCS()
        {
            try
            {
                OrdenActual();
                Orden.Guardar();
                LoadOrdenesCS();

            }
            catch (Exception exc)
            {
                MessageBox.Show("Verifique la información general pues es errónea" + exc.Message,
                      "ProjectA", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OrdenActual()
        {
            Orden = new cOrdenCompraServicio();

            if (tbOrdenesID.Text == "")
                Orden.ModoActaul = cOrdenCompraServicio.Modo.Adiciona;
            else
                Orden.RegistroID = int.Parse(tbOrdenesID.Text);

            Orden.NumeroOrden = int.Parse(tbOCOSNoOrden.Text);
            Orden.ProveedorID = int.Parse(cbOrdenesProveedor.SelectedValue.ToString());
            Orden.Descripcion = tbOrdenDescripcion.Text;
            Orden.FechaEntrega = dtpOrdenEntrega.Value;
            Orden.Valor = float.Parse(tbOrdenValor.Text);
            Orden.EsServicio = chbOSOCEsServicio.Checked;
            Orden.NoEntegas = (int)nudOrdenNoEntregas.Value;
            for (int iFila = 0; iFila < dgvElementosOrden.Rows.Count; iFila++)
            {
                DataRow dr = Orden.ElementosOrden.NewRow();
                dr["ID"] = dgvElementosOrden.Rows[iFila].Cells[4].Value;
                dr["ItemsID"] = dgvElementosOrden.Rows[iFila].Cells[6].Value;
                dr["Cantidad"] = dgvElementosOrden.Rows[iFila].Cells[1].Value;
                dr["PrecioUD"] = dgvElementosOrden.Rows[iFila].Cells[2].Value;
                dr["PrecioTotal"] = dgvElementosOrden.Rows[iFila].Cells[3].Value;
                Orden.ElementosOrden.Rows.Add(dr);
            }
        }

        private void CalculoAcumulado()
        {
            TotalAcumuladoElementosOrdenesCompraServicios = 0;
            for (int iRows = 0; iRows < dgvElementosOrden.Rows.Count; iRows++)
            {
                TotalAcumuladoElementosOrdenesCompraServicios = TotalAcumuladoElementosOrdenesCompraServicios + float.Parse(dgvElementosOrden.Rows[iRows].Cells["precioTotal"].Value.ToString());
            }
        }

        private void btCostosOCOS_Click(object sender, EventArgs e)
        {
            frmPrincipal.cPage_General.SubPage_Costos.WindowFacturas.panelCostosFacturasVer.Visible = false;
            frmPrincipal.cPage_General.SubPage_Costos.WindowFacturas.panelCostosFacturasAgregar.Visible = false;
            panelCostosOCOS.Visible = true;
        }

        private void panelCostosOCOS_VisibleChanged(object sender, EventArgs e)
        {
            if (panelCostosOCOS.Visible)
            {
                PestañaActual = PestañaModuloCostos.Ordenes;
                LoadOrdenesCS();
            }

        }

        private int FilaActialElementosOrdenes { get; set; }
        private void btEliminarElementoOrdenesCS_Click(object sender, EventArgs e)
        {
            string sErr = "";
            if (dgvElementosOrden.Rows[FilaActialElementosOrdenes].Cells[4].Value.ToString() == "")
            {
                dgvElementosOrden.Rows.Remove(dgvElementosOrden.Rows[FilaActialElementosOrdenes]);
            }

            else
            {
                if (wsGeneral.DeleteItemsPorOrden(int.Parse(dgvElementosOrden.Rows[FilaActialElementosOrdenes].Cells[4].Value.ToString()), out sErr))
                {
                    MessageBox.Show("Eliminación Exitosa");
                }
            }

            CalculoAcumulado();
        }


        private void btTabInformacionGen_CheckedChanged(object sender, EventArgs e)
        {
            if (btTabInformacionGen.Checked)
            {
                this.panelInformacionGeneral.Visible = true;
                this.panelElementosOrden.Visible = false;
                this.btTabElemOrden.Checked = false;

                btTabInformacionGen.Enabled = false;
                btTabElemOrden.Enabled = true;
            }
        }
        private void btTabElemOrden_CheckedChanged(object sender, EventArgs e)
        {
            if (btTabElemOrden.Checked)
            {
                this.panelElementosOrden.Visible = true;
                this.panelInformacionGeneral.Visible = false;
                this.btTabInformacionGen.Checked = false;

                btTabElemOrden.Enabled = false;
                btTabInformacionGen.Enabled = true;
            }
        }

        private void dgvElementosOrden_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            FilaActialElementosOrdenes = e.RowIndex;
        }

        private void dgvElementosOrden_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1)
                {
                    if ((e.ColumnIndex == 1) || (e.ColumnIndex == 2))
                    {
                        float.Parse(dgvElementosOrden.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                        if ((dgvElementosOrden.Rows[e.RowIndex].Cells[1].Value != null) && (dgvElementosOrden.Rows[e.RowIndex].Cells[2].Value != null))
                        {
                            dgvElementosOrden.Rows[e.RowIndex].Cells[3].Value = (object)((float.Parse(dgvElementosOrden.Rows[e.RowIndex].Cells[1].Value.ToString()) * float.Parse(dgvElementosOrden.Rows[e.RowIndex].Cells[2].Value.ToString())));
                            CalculoAcumulado();
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        void toolStripButton1_Click(object sender, EventArgs e)
        {
            dsPrincipal.ElementosAsociadosOrdenes.Clear();
            TotalOrdenCS = 0;
            TotalAcumuladoElementosOrdenesCompraServicios = 0;
            nudOrdenNoEntregas.Value = 0;

        }

        private void tsbExportarOC_Click(object sender, EventArgs e)
        {
            Excel.ExportarAArchivo(dsPrincipal.Tables["OrdenCompraServicios"]);
        }

        private void tsbImportarOC_Click(object sender, EventArgs e)
        {
            DataSet datos = new DataSet();
            datos = Excel.ImportarOrdenesDesdeArchivo();
            wsGeneral.AgregarOrdenesDesdeArchivo(CentrosCostosId, datos);
        }

        #region Creación de controles

        private void InitComponent()
        {
            this.resources = new System.ComponentModel.ComponentResourceManager(typeof(frmPrincipal));

        
            // ----------------------------------------------------------------------------------------------------------------------------------------------------------
            // Window panelCostosOCOS
            // ----------------------------------------------------------------------------------------------------------------------------------------------------------
            this.panelCostosOCOS.Location = new Point(0, 0);
            this.lbCostoOSOC.Location = new Point(12,50);

            this.btTabInformacionGen = new cButtonToggle(new Point(301, 14), new Size(162, 39), new EventHandler(btTabInformacionGen_CheckedChanged), Properties.Resources.bt_subtag_info_1, Properties.Resources.bt_subtag_info_2, Properties.Resources.bt_subtag_info_3, Properties.Resources.bt_subtag_info_4);
            this.btTabElemOrden = new cButtonToggle(new Point(463, 14), new Size(162, 39), new EventHandler(btTabElemOrden_CheckedChanged), Properties.Resources.bt_subtag_elementos_1, Properties.Resources.bt_subtag_elementos_2, Properties.Resources.bt_subtag_elementos_3, Properties.Resources.bt_subtag_elementos_4);

            // panel panelInformacionGeneral
            this.panelInformacionGeneral.Location = new Point(323,53);
            this.panelInformacionGeneral.Visible = false;

            this.lNoOrden = new cLabel(new Point(31, 34), new Size(87, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "No. Orden:");
            this.lProveedor = new cLabel(new Point(31, 76), new Size(90, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "Proveedor:");
            this.lDescripcion = new cLabel(new Point(31, 115), new Size(97, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "Descripción:");
            this.lOrdenFecha = new cLabel(new Point(31, 156), new Size(139, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "Fecha de Entrega:");
            this.lNumeroEntregas = new cLabel(new Point(31, 191), new Size(159, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "Número de Entregas:");
            this.lOCOSValor = new cLabel(new Point(31, 229), new Size(171, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "Valor:                             $");
            this.lOCOSTipoOrden = new cLabel(new Point(31, 266), new Size(115, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "Tipo de Orden:");
           
            this.tbOCOSNoOrden = new cTextBox(new Point(206, 32), new Size(208, 30), new Font("Arial", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), null, null);
            this.tbOrdenesID = new cTextBox(new Point(206, 32), new Size(1, 1), new Font("Arial", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), null, null);
            
            
            
            this.tbOCOSNoOrden.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bsOrdenes, "NumPoSo", true));


            this.tbOrdenDescripcion = new cTextBox(new Point(206, 112), new Size(208, 25), new Font("Arial", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), null, null);
            this.tbOrdenDescripcion.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bsOrdenes, "Descripcion", true));
            
            this.tbOrdenValor = new cTextBox(new Point(206, 228), new Size(208, 25), new Font("Arial", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), null, null);
            this.tbOrdenValor.Text = "0";
            this.tbOrdenValor.TextAlign = HorizontalAlignment.Right;
            this.tbOrdenValor.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bsOrdenes, "Valor", true));

            this.cbOrdenesProveedor.Location = new Point(206,73);
            this.chbOSOCEsServicio.Location = new Point(206,263);
            this.btSalvarOrdenesCS = new cButton(new Point(182, 303), new Size(119, 46), new EventHandler(this.btSalvarOrdenesCS_Click), Properties.Resources.bt_tag_opciones_ok_1, Properties.Resources.bt_tag_opciones_ok_2, Properties.Resources.bt_tag_opciones_ok_3, Properties.Resources.bt_tag_opciones_ok_4);

            this.panelInformacionGeneral.Controls.Add(this.lNoOrden);
            this.panelInformacionGeneral.Controls.Add(this.lProveedor);
            this.panelInformacionGeneral.Controls.Add(this.lDescripcion);
            this.panelInformacionGeneral.Controls.Add(this.lOCOSTipoOrden);
            this.panelInformacionGeneral.Controls.Add(this.lOCOSValor);
            this.panelInformacionGeneral.Controls.Add(this.tbOCOSNoOrden);
            this.panelInformacionGeneral.Controls.Add(this.tbOrdenDescripcion);
            this.panelInformacionGeneral.Controls.Add(this.tbOrdenValor);
            this.panelInformacionGeneral.Controls.Add(this.cbOrdenesProveedor);
            this.panelInformacionGeneral.Controls.Add(this.chbOSOCEsServicio);
            this.panelInformacionGeneral.Controls.Add(this.lOrdenFecha);
            this.panelInformacionGeneral.Controls.Add(this.lNumeroEntregas);
            this.panelInformacionGeneral.Controls.Add(this.dtpOrdenEntrega);
            this.panelInformacionGeneral.Controls.Add(this.nudOrdenNoEntregas);
            this.panelInformacionGeneral.Controls.Add(this.btSalvarOrdenesCS);
            this.panelInformacionGeneral.Controls.Add(this.tbOrdenesID);

            // panel panelElementosOrden
            this.panelElementosOrden.Location = new Point(323, 53);
            //this.panelElementosOrden.Visible = false;

            this.dgvElementosOrden.Location = new Point(0,73);
            this.label14 = new cLabel(new Point(0, 14), new Size(127, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "Total Acumulado:");
            this.label15 = new cLabel(new Point(260, 14), new Size(80, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "Restante :");
            this.lTotalAcumuladoValor = new cLabel(new Point(140, 14), new Size(100, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "$ 0");
            this.lOCSValorRestante = new cLabel(new Point(350, 14), new Size(100, 18), new Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154))))), ContentAlignment.TopLeft, "$ 0");
            this.btAgregarElementoOrdenCS = new cButton(new Point(190, 40), new Size(99, 33), new EventHandler(this.btAgregarElementoOrdenCS_Click), Properties.Resources.bt_general_1, Properties.Resources.bt_general_2, Properties.Resources.bt_general_3, Properties.Resources.bt_general_4);
            this.btAgregarElementoOrdenCS.Font = new System.Drawing.Font("Arial Black", 10F, System.Drawing.FontStyle.Bold);
            this.btAgregarElementoOrdenCS.ForeColor = System.Drawing.Color.White;
            this.btEliminarElementoOrdenesCS = new cButton(new Point(345, 40), new Size(99, 33), new EventHandler(this.btEliminarElementoOrdenesCS_Click), Properties.Resources.bt_subtag_eliminar_1, Properties.Resources.bt_subtag_eliminar_2, Properties.Resources.bt_subtag_eliminar_3, Properties.Resources.bt_subtag_eliminar_4);
            this.cbItemsEstructura = new cComboBox(new Point(0, 45), new Size(130, 20));
            
            
            this.panelElementosOrden.Controls.Add(this.dgvElementosOrden);
            this.panelElementosOrden.Controls.Add(this.label14);
            this.panelElementosOrden.Controls.Add(this.label15);
            this.panelElementosOrden.Controls.Add(this.lTotalAcumuladoValor);
            this.panelElementosOrden.Controls.Add(this.lOCSValorRestante);
            this.panelElementosOrden.Controls.Add(this.btAgregarElementoOrdenCS);
            this.panelElementosOrden.Controls.Add(this.btEliminarElementoOrdenesCS);
            this.panelElementosOrden.Controls.Add(this.cbItemsEstructura);

            // General
            this.panelCostosOCOS.Controls.Add(this.lbCostoOSOC);
            this.panelCostosOCOS.Controls.Add(this.panelInformacionGeneral);
            this.panelCostosOCOS.Controls.Add(this.panelElementosOrden);
            this.panelCostosOCOS.Controls.Add(this.btTabInformacionGen);
            this.panelCostosOCOS.Controls.Add(this.btTabElemOrden);

            // 
            // tbOrdenesID
            // 
            this.tbOrdenesID.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bsOrdenes, "ID", true));
            this.tbOrdenesID.Font = new System.Drawing.Font("Arial", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbOrdenesID.Margin = new System.Windows.Forms.Padding(0);
            this.tbOrdenesID.MaxLength = 30;
            this.tbOrdenesID.Name = "tbOrdenesID";
            this.tbOrdenesID.Size = new System.Drawing.Size(1, 1);
            this.tbOrdenesID.TabIndex = 63;

            // BindingNavigator bindingNavigator1
            #region bindingNavigator1
            this.bindingNavigator1 = new BindingNavigator(new Container());
            this.toolStripButton2 = new ToolStripButton();
            this.toolStripSeparator7 = new ToolStripSeparator();
            this.toolStripTextBox2 = new ToolStripTextBox();
            this.toolStripLabel2 = new ToolStripLabel();
            this.toolStripSeparator8 = new ToolStripSeparator();
            this.toolStripButton5 = new ToolStripButton();
            this.toolStripSeparator9 = new ToolStripSeparator();
            this.toolStripButton1 = new ToolStripButton();
            this.toolStripButton6 = new ToolStripButton();
            this.tsbImportarOC = new ToolStripButton();
            ((ISupportInitialize)(this.bindingNavigator1)).BeginInit();
            this.bindingNavigator1.SuspendLayout();
            // 
            // bnCostosFacturasVer
            // 
            this.bindingNavigator1.AddNewItem = this.toolStripButton1;
            this.bindingNavigator1.AutoSize = false;
            this.bindingNavigator1.BindingSource = this.bsOrdenes;
            this.bindingNavigator1.CountItem = this.toolStripLabel2;
            this.bindingNavigator1.DeleteItem = null;
            this.bindingNavigator1.Dock = System.Windows.Forms.DockStyle.None;
            this.bindingNavigator1.Font = new Font("Arial", 11.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
            this.bindingNavigator1.GripMargin = new System.Windows.Forms.Padding(0);
            this.bindingNavigator1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.bindingNavigator1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripButton2,
            this.toolStripSeparator7,
            this.toolStripTextBox2,
            this.toolStripLabel2,
            this.toolStripSeparator8,
            this.toolStripButton5,
            this.toolStripSeparator9,
            this.toolStripButton1,
            this.toolStripButton6, 
            this.tsbImportarOC});
            this.bindingNavigator1.Location = new Point(12, 10);
            this.bindingNavigator1.MoveFirstItem = null;
            this.bindingNavigator1.MoveLastItem = null;
            this.bindingNavigator1.MoveNextItem = this.toolStripButton5;
            this.bindingNavigator1.MovePreviousItem = this.toolStripButton2;
            this.bindingNavigator1.Name = "bindingNavigator1";
            this.bindingNavigator1.Padding = new System.Windows.Forms.Padding(0);
            this.bindingNavigator1.PositionItem = this.toolStripTextBox2;
            this.bindingNavigator1.Size = new Size(280, 30);
            this.bindingNavigator1.Text = "bindingNavigator1";
            // 
            // toolStripButton2
            // 
            this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButton2.Image = ((System.Drawing.Image)(Properties.Resources.bindingNavigatorMovePreviousItem_Image));
            this.toolStripButton2.Name = "toolStripButton2";
            this.toolStripButton2.RightToLeftAutoMirrorImage = true;
            this.toolStripButton2.Size = new System.Drawing.Size(23, 27);
            this.toolStripButton2.Text = "Mover anterior";
            // 
            // toolStripSeparator7
            // 
            this.toolStripSeparator7.Name = "toolStripSeparator7";
            this.toolStripSeparator7.Size = new System.Drawing.Size(6, 30);
            // 
            // toolStripTextBox2
            // 
            this.toolStripTextBox2.AccessibleName = "Posición";
            this.toolStripTextBox2.AutoSize = false;
            this.toolStripTextBox2.Name = "toolStripTextBox2";
            this.toolStripTextBox2.Size = new System.Drawing.Size(50, 23);
            this.toolStripTextBox2.Text = "0";
            this.toolStripTextBox2.ToolTipText = "Posición actual";
            // 
            // toolStripLabel2
            // 
            this.toolStripLabel2.Name = "toolStripLabel2";
            this.toolStripLabel2.Size = new System.Drawing.Size(37, 27);
            this.toolStripLabel2.Text = "de {0}";
            this.toolStripLabel2.ToolTipText = "Número total de elementos";
            // 
            // toolStripSeparator8
            // 
            this.toolStripSeparator8.Name = "toolStripSeparator8";
            this.toolStripSeparator8.Size = new System.Drawing.Size(6, 30);
            // 
            // toolStripButton5
            // 
            this.toolStripButton5.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButton5.Image = ((System.Drawing.Image)(Properties.Resources.bindingNavigatorMoveNextItem_Image));
            this.toolStripButton5.Name = "toolStripButton5";
            this.toolStripButton5.RightToLeftAutoMirrorImage = true;
            this.toolStripButton5.Size = new System.Drawing.Size(23, 27);
            this.toolStripButton5.Text = "Mover siguiente";
            // 
            // toolStripSeparator9
            // 
            this.toolStripSeparator9.Name = "toolStripSeparator9";
            this.toolStripSeparator9.Size = new System.Drawing.Size(6, 30);
            // 
            // toolStripButton1
            // 
            this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButton1.Image = ((System.Drawing.Image)(Properties.Resources.bindingNavigatorAddNewItem_Image));
            this.toolStripButton1.Name = "toolStripButton1";
            this.toolStripButton1.RightToLeftAutoMirrorImage = true;
            this.toolStripButton1.Size = new System.Drawing.Size(23, 27);
            this.toolStripButton1.Text = "Agregar nuevo";
            this.toolStripButton1.Click += new EventHandler(toolStripButton1_Click);
            // 
            // toolStripButton6
            // 
            this.toolStripButton6.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripButton6.Image = ((System.Drawing.Image)(Properties.Resources.bindingNavigatorDeleteItem_Image)); 
            this.toolStripButton6.Name = "toolStripButton6";
            this.toolStripButton6.RightToLeftAutoMirrorImage = true;
            this.toolStripButton6.Size = new System.Drawing.Size(23, 27);
            this.toolStripButton6.Text = "Eliminar";
            this.toolStripButton6.Click += new EventHandler(toolStripButton6_Click);
            // 
            // tsbImportarOC
            // 
            this.tsbImportarOC.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.tsbImportarOC.Image = global::ProjectA.Properties.Resources.bt_importar;
            this.tsbImportarOC.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.tsbImportarOC.Name = "tsbImportarOC";
            this.tsbImportarOC.Size = new System.Drawing.Size(23, 22);
            this.tsbImportarOC.Text = "Importar Factura";
            this.tsbImportarOC.Click += new EventHandler(tsbImportarOC_Click);
           
            ((System.ComponentModel.ISupportInitialize)(this.bindingNavigator1)).EndInit();
            this.bindingNavigator1.ResumeLayout(false);
            this.bindingNavigator1.PerformLayout();
            #endregion

            this.panelCostosOCOS.Controls.Add(this.bindingNavigator1);
            this.panelCostosOCOS.VisibleChanged += new EventHandler(panelCostosOCOS_VisibleChanged);

            this.Controls.Add(this.panelCostosOCOS);

        }

        void toolStripButton6_Click(object sender, EventArgs e)
        {
            DialogResult Dialog = new DialogResult();
            Dialog = MessageBox.Show("¿ Esta seguro que desea eliminar la orden No. " + tbOCOSNoOrden.Text + " ?",
                      "ProjectA", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (Dialog == System.Windows.Forms.DialogResult.Yes)
            {
                OrdenActual();
                Orden.Eliminar();
                LoadOrdenesCS();

            }
        }

        

        public cButtonToggle btTabInformacionGen { get; set; }
        public cButtonToggle btTabElemOrden { get; set; }

        public TextBox tbOrdenesID { get; set; }

        // controles del panelElementosOrden
        public cLabel label14 { get; set; }
        public cLabel label15 { get; set; }
        public cLabel lTotalAcumuladoValor { get; set; }
        public cLabel lOCSValorRestante { get; set; }
        public cButton btAgregarElementoOrdenCS { get; set; }
        public cButton btEliminarElementoOrdenesCS { get; set; }

        // Controles del panelInformacionGeneral
        public cLabel lNoOrden { get; set; }
        public cLabel lProveedor { get; set; }
        public cLabel lDescripcion { get; set; }
        public cLabel lOCOSTipoOrden { get; set; }
        public cLabel lOCOSValor { get; set; }
        public cTextBox tbOCOSNoOrden { get; set; }
        public cTextBox tbOrdenDescripcion { get; set; }
        public cTextBox tbOrdenValor { get; set; }
        public cLabel lOrdenFecha { get; set; }
        public cLabel lNumeroEntregas { get; set; }
        public cButton btSalvarOrdenesCS { get; set; } 

        //bindingNavigator1
        private BindingNavigator bindingNavigator1;
        private ToolStripButton toolStripButton1;
        private ToolStripLabel toolStripLabel2;
        private ToolStripButton toolStripButton6;
        private ToolStripButton toolStripButton2;
        private ToolStripSeparator toolStripSeparator7;
        private ToolStripTextBox toolStripTextBox2;
        private ToolStripSeparator toolStripSeparator8;
        private ToolStripButton toolStripButton5;
        private ToolStripSeparator toolStripSeparator9;
        private ToolStripButton tsbImportarOC;
        private BindingSource bsProveedores;
        private BindingSource bsElementosOrdenes;
        private IContainer components;
        private DataSets.dsPrincipal dsPrincipal;
        private cListBox lbCostoOSOC;
        private cWindow panelInformacionGeneral;
        private cWindow panelElementosOrden;
        private cComboBox cbOrdenesProveedor;
        private cComboBox cbItemsEstructura;
        private cCheckBox chbOSOCEsServicio;
        private DataGridView dgvElementosOrden;
        private DataGridViewTextBoxColumn descripcionDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn cantidadDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn precioUDDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn precioTotal;
        private DataGridViewTextBoxColumn iDDataGridViewTextBoxColumn1;
        private DataGridViewTextBoxColumn poSoIDDataGridViewTextBoxColumn;
        private DataGridViewTextBoxColumn itemIDDataGridViewTextBoxColumn;
        private cDateTimePicker dtpOrdenEntrega;
        private NumericUpDown nudOrdenNoEntregas;

        public frmPrincipal frmPrincipal { get; set; }

        private ComponentResourceManager resources;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.panelCostosOCOS = new ProjectA.Controls.cWindow();
            this.bsProveedores = new System.Windows.Forms.BindingSource(this.components);
            this.bsElementosOrdenes = new System.Windows.Forms.BindingSource(this.components);
            this.dsPrincipal = new ProjectA.DataSets.dsPrincipal();
            this.lbCostoOSOC = new ProjectA.Controls.cListBox();
            this.panelInformacionGeneral = new ProjectA.Controls.cWindow();
            this.panelElementosOrden = new ProjectA.Controls.cWindow();
            this.cbOrdenesProveedor = new ProjectA.Controls.cComboBox();
            this.chbOSOCEsServicio = new ProjectA.Controls.cCheckBox();
            this.dgvElementosOrden = new System.Windows.Forms.DataGridView();
            this.descripcionDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.cantidadDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.precioUDDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.precioTotal = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.iDDataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.poSoIDDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.itemIDDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.dtpOrdenEntrega = new ProjectA.Controls.cDateTimePicker();
            this.nudOrdenNoEntregas = new System.Windows.Forms.NumericUpDown();
            this.bsOrdenes = new System.Windows.Forms.BindingSource(this.components);
            this.cbItemsEstructura = new cComboBox();
            ((System.ComponentModel.ISupportInitialize)(this.bsProveedores)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bsElementosOrdenes)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dsPrincipal)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgvElementosOrden)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.nudOrdenNoEntregas)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bsOrdenes)).BeginInit();
            this.SuspendLayout();
            // 
            // panelCostosOCOS
            // 
            this.panelCostosOCOS.ImageBackGroundPage = global::ProjectA.Properties.Resources.bg_sub_win_oc_os;
            this.panelCostosOCOS.Location = new System.Drawing.Point(0, 0);
            this.panelCostosOCOS.Margin = new System.Windows.Forms.Padding(0);
            this.panelCostosOCOS.Name = "panelCostosOCOS";
            this.panelCostosOCOS.Size = new System.Drawing.Size(801, 426);
            this.panelCostosOCOS.TabIndex = 0;
            // 
            // bsProveedores
            // 
            this.bsProveedores.DataMember = "Proveedores";
            this.bsProveedores.DataSource = this.dsPrincipal;
            // 
            // dsPrincipal
            // 
            this.dsPrincipal.DataSetName = "dsPrincipal";
            this.dsPrincipal.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            // 
            // lbCostoOSOC
            // 
            this.lbCostoOSOC.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.lbCostoOSOC.DataSource = this.bsOrdenes;
            this.lbCostoOSOC.DisplayMember = "Descripcion";
            this.lbCostoOSOC.Font = new System.Drawing.Font("Arial", 11.25F);
            this.lbCostoOSOC.FormattingEnabled = true;
            this.lbCostoOSOC.Location = new System.Drawing.Point(0, 0);
            this.lbCostoOSOC.Margin = new System.Windows.Forms.Padding(0);
            this.lbCostoOSOC.Name = "lbCostoOSOC";
            this.lbCostoOSOC.Size = new System.Drawing.Size(280, 356);
            this.lbCostoOSOC.TabIndex = 0;
            // 
            // panelInformacionGeneral
            // 
            this.panelInformacionGeneral.ImageBackGroundPage = null;
            this.panelInformacionGeneral.Location = new System.Drawing.Point(0, 0);
            this.panelInformacionGeneral.Margin = new System.Windows.Forms.Padding(0);
            this.panelInformacionGeneral.Name = "panelInformacionGeneral";
            this.panelInformacionGeneral.Size = new System.Drawing.Size(446, 360);
            this.panelInformacionGeneral.TabIndex = 0;
            // 
            // panelElementosOrden
            // 
            this.panelElementosOrden.ImageBackGroundPage = null;
            this.panelElementosOrden.Location = new System.Drawing.Point(0, 0);
            this.panelElementosOrden.Margin = new System.Windows.Forms.Padding(0);
            this.panelElementosOrden.Name = "panelElementosOrden";
            this.panelElementosOrden.Size = new System.Drawing.Size(446, 360);
            this.panelElementosOrden.TabIndex = 0;
            // 
            // cbOrdenesProveedor
            //
            this.cbOrdenesProveedor.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bsOrdenes, "ProviderID", true));
            this.cbOrdenesProveedor.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bsProveedores, "NameProvider", true));
            this.cbOrdenesProveedor.DataSource = this.bsProveedores;
            this.cbOrdenesProveedor.DisplayMember = "NameProvider";
            this.cbOrdenesProveedor.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbOrdenesProveedor.DropDownWidth = 208;
            this.cbOrdenesProveedor.Font = new System.Drawing.Font("Arial", 11.25F);
            this.cbOrdenesProveedor.FormattingEnabled = true;
            this.cbOrdenesProveedor.Location = new System.Drawing.Point(0, 0);
            this.cbOrdenesProveedor.Name = "cbOrdenesProveedor";
            this.cbOrdenesProveedor.Size = new System.Drawing.Size(208, 25);
            this.cbOrdenesProveedor.TabIndex = 0;
            this.cbOrdenesProveedor.ValueMember = "ID";
            // 
            // cbItemsEstructura
            //
            this.cbItemsEstructura.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbItemsEstructura.DropDownWidth = 208;
            this.cbItemsEstructura.Font = new System.Drawing.Font("Arial", 11.25F);
            this.cbItemsEstructura.FormattingEnabled = true;
            this.cbItemsEstructura.Name = "cbItemsEstructura";
            this.cbItemsEstructura.TabIndex = 0;
            // 
            // chbOSOCEsServicio
            // 
            this.chbOSOCEsServicio.DataBindings.Add(new System.Windows.Forms.Binding("Checked", this.bsOrdenes, "Tipo", true));
            this.chbOSOCEsServicio.AutoSize = true;
            this.chbOSOCEsServicio.Font = new System.Drawing.Font("Arial", 11.25F, System.Drawing.FontStyle.Bold);
            this.chbOSOCEsServicio.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(75)))), ((int)(((byte)(117)))), ((int)(((byte)(154)))));
            this.chbOSOCEsServicio.Location = new System.Drawing.Point(0, 0);
            this.chbOSOCEsServicio.Margin = new System.Windows.Forms.Padding(0);
            this.chbOSOCEsServicio.Name = "chbOSOCEsServicio";
            this.chbOSOCEsServicio.Size = new System.Drawing.Size(86, 22);
            this.chbOSOCEsServicio.TabIndex = 0;
            this.chbOSOCEsServicio.Text = "Servicio";
            this.chbOSOCEsServicio.UseVisualStyleBackColor = true;
            this.dgvElementosOrden.AllowUserToAddRows = false;
            this.dgvElementosOrden.AllowUserToDeleteRows = false;
            this.dgvElementosOrden.AutoGenerateColumns = false;
            this.dgvElementosOrden.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvElementosOrden.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.descripcionDataGridViewTextBoxColumn,
            this.cantidadDataGridViewTextBoxColumn,
            this.precioUDDataGridViewTextBoxColumn,
            this.precioTotal,
            this.iDDataGridViewTextBoxColumn1,
            this.poSoIDDataGridViewTextBoxColumn,
            this.itemIDDataGridViewTextBoxColumn});
            this.dgvElementosOrden.DataSource = this.bsElementosOrdenes;
            this.dgvElementosOrden.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.dgvElementosOrden.Location = new System.Drawing.Point(0, 87);
            this.dgvElementosOrden.Name = "dgvElementosOrden";
            this.dgvElementosOrden.Size = new System.Drawing.Size(446, 280);
            this.dgvElementosOrden.TabIndex = 9;
            this.dgvElementosOrden.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvElementosOrden_CellClick);
            this.dgvElementosOrden.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvElementosOrden_CellValueChanged);
            // 
            // descripcionDataGridViewTextBoxColumn
            // 
            this.descripcionDataGridViewTextBoxColumn.DataPropertyName = "Descripcion";
            this.descripcionDataGridViewTextBoxColumn.HeaderText = "Elemento Estructura";
            this.descripcionDataGridViewTextBoxColumn.Name = "descripcionDataGridViewTextBoxColumn";
            // 
            // cantidadDataGridViewTextBoxColumn
            // 
            this.cantidadDataGridViewTextBoxColumn.DataPropertyName = "Cantidad";
            this.cantidadDataGridViewTextBoxColumn.HeaderText = "Cantidad";
            this.cantidadDataGridViewTextBoxColumn.Name = "cantidadDataGridViewTextBoxColumn";
            // 
            // precioUDDataGridViewTextBoxColumn
            // 
            this.precioUDDataGridViewTextBoxColumn.DataPropertyName = "PrecioUD";
            this.precioUDDataGridViewTextBoxColumn.HeaderText = "Precio Unidad";
            this.precioUDDataGridViewTextBoxColumn.Name = "precioUDDataGridViewTextBoxColumn";
            // 
            // precioTotal
            // 
            this.precioTotal.DataPropertyName = "PrecioTotal";
            this.precioTotal.HeaderText = "Precio Total";
            this.precioTotal.Name = "precioTotal";
            // 
            // iDDataGridViewTextBoxColumn1
            // 
            this.iDDataGridViewTextBoxColumn1.DataPropertyName = "ID";
            this.iDDataGridViewTextBoxColumn1.HeaderText = "ID";
            this.iDDataGridViewTextBoxColumn1.Name = "iDDataGridViewTextBoxColumn1";
            this.iDDataGridViewTextBoxColumn1.Visible = false;
            // 
            // poSoIDDataGridViewTextBoxColumn
            // 
            this.poSoIDDataGridViewTextBoxColumn.DataPropertyName = "PoSoID";
            this.poSoIDDataGridViewTextBoxColumn.HeaderText = "PoSoID";
            this.poSoIDDataGridViewTextBoxColumn.Name = "poSoIDDataGridViewTextBoxColumn";
            this.poSoIDDataGridViewTextBoxColumn.Visible = false;
            // 
            // itemIDDataGridViewTextBoxColumn
            // 
            this.itemIDDataGridViewTextBoxColumn.DataPropertyName = "ItemID";
            this.itemIDDataGridViewTextBoxColumn.HeaderText = "ItemID";
            this.itemIDDataGridViewTextBoxColumn.Name = "itemIDDataGridViewTextBoxColumn";
            this.itemIDDataGridViewTextBoxColumn.Visible = false;
            // 
            // bsElementosOrdenes
            // 
            this.bsElementosOrdenes.DataMember = "ElementosAsociadosOrdenes";
            this.bsElementosOrdenes.DataSource = this.dsPrincipal;
            // 
            // dtpOrdenEntrega
            // 
            this.dtpOrdenEntrega.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bsOrdenes, "Fecha", true));
            this.dtpOrdenEntrega.CalendarFont = new System.Drawing.Font("Arial", 11.25F);
            this.dtpOrdenEntrega.Font = new System.Drawing.Font("Arial", 11.25F);
            this.dtpOrdenEntrega.Format = System.Windows.Forms.DateTimePickerFormat.Short;
            this.dtpOrdenEntrega.Location = new System.Drawing.Point(206, 150);
            this.dtpOrdenEntrega.Name = "dtpOrdenEntrega";
            this.dtpOrdenEntrega.Size = new System.Drawing.Size(208, 25);
            this.dtpOrdenEntrega.TabIndex = 0;
            // 
            // nudOrdenNoEntregas
            // 
            this.nudOrdenNoEntregas.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bsOrdenes, "NoEntregas", true));
            this.nudOrdenNoEntregas.Font = new System.Drawing.Font("Arial", 11.25F);
            this.nudOrdenNoEntregas.Location = new System.Drawing.Point(206, 189);
            this.nudOrdenNoEntregas.Name = "nudOrdenNoEntregas";
            this.nudOrdenNoEntregas.Size = new System.Drawing.Size(208, 25);
            this.nudOrdenNoEntregas.TabIndex = 0;
            // 
            // bsOrdenes
            // 
            this.bsOrdenes.AllowNew = true;
            this.bsOrdenes.DataMember = "OrdenCompraServicios";
            this.bsOrdenes.DataSource = this.dsPrincipal;
            this.bsOrdenes.CurrentChanged += new System.EventHandler(this.bsOrdenes_CurrentChanged);
            ((System.ComponentModel.ISupportInitialize)(this.bsProveedores)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bsElementosOrdenes)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dsPrincipal)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgvElementosOrden)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.nudOrdenNoEntregas)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bsOrdenes)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private void ArmandoEstructura(int indicePadre, string sEspace)
        {
            try
            {

                // Crear un DataView con los Nodos que dependen del Nodo padre pasado como parámetro.
                DataView dataViewHijos = new DataView(dsEstructura.Tables[0]);
                dataViewHijos.RowFilter = dsEstructura.Tables[0].Columns["MaestroID"].ColumnName + " = " + indicePadre;
                if (indicePadre != 0)
                    sEspace = sEspace + " ";

                // Agregar al TreeView los nodos Hijos que se han obtenido en el DataView.
                foreach (DataRowView dataRowCurrent in dataViewHijos)
                {
                    string sName = sEspace + dataRowCurrent["Descripcion"].ToString();
                    Item Item = new cWindowOC_OS.Item();
                    Item.ItemId = int.Parse(dataRowCurrent["ID"].ToString());
                    Item.Name = sName;
                    Items.Add(Item);


                    // Llamada recurrente al mismo método para agregar los Hijos del Nodo recién agregado.
                    ArmandoEstructura(Int32.Parse(dataRowCurrent["ID"].ToString()), sEspace);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        class Item
        {
            public int ItemId { get; set; }
            public string Name { get; set; }
        }
    }
}
